package com.budwk.app.waterquality.services.impl;

import cn.hutool.core.collection.CollectionUtil;
import com.budwk.app.sys.models.Sys_user;
import com.budwk.app.utils.PoiUtil;
import com.budwk.app.waterquality.component.CommonComponent;
import com.budwk.app.waterquality.config.AppException;
import com.budwk.app.waterquality.models.ScoreInfo;
import com.budwk.app.waterquality.vo.QueryCommonParamVo;
import com.budwk.app.waterquality.vo.TemplateConfigQueryVo;
import com.budwk.starter.common.page.Pagination;
import com.budwk.starter.database.service.BaseServiceImpl;
import com.budwk.app.waterquality.models.FormsTemplateConfig;
import com.budwk.app.waterquality.services.FormsTemplateConfigService;
import org.apache.commons.lang3.StringUtils;
import org.apache.poi.ss.usermodel.CellStyle;
import org.apache.poi.xssf.streaming.SXSSFCell;
import org.apache.poi.xssf.streaming.SXSSFRow;
import org.apache.poi.xssf.streaming.SXSSFSheet;
import org.apache.poi.xssf.streaming.SXSSFWorkbook;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.nutz.dao.Cnd;
import org.nutz.dao.Dao;
import org.nutz.dao.util.cri.SqlExpressionGroup;
import org.nutz.ioc.loader.annotation.IocBean;

import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.Collections;
import java.util.List;

@IocBean(args = {"refer:dao"})
public class FormsTemplateConfigServiceImpl extends BaseServiceImpl<FormsTemplateConfig> implements FormsTemplateConfigService {
    public FormsTemplateConfigServiceImpl(Dao dao) {
        super(dao);
    }

    @Override
    public Pagination listByPage(TemplateConfigQueryVo req) {
        Cnd cnd = buildQueryCondition(req);
        Pagination pagination = this.listPage(req.getPageNo(), req.getPageSize(), cnd);
        return pagination;
    }

    private Cnd buildQueryCondition(TemplateConfigQueryVo req) {
        Cnd cnd = Cnd.NEW();
        SqlExpressionGroup where = cnd.where();
        if (StringUtils.isNotEmpty(req.getFName())) {
            where.andLike(FormsTemplateConfig::getFName, req.getFName());
        }
        if (StringUtils.isNotEmpty(req.getFNumber())) {
            where.andLike(FormsTemplateConfig::getFNumber, req.getFNumber());
        }
        cnd.getOrderBy().desc(FormsTemplateConfig::getCreateDate);
        return cnd;
    }



    @Override
    public void create(FormsTemplateConfig c) {
        Sys_user user = CommonComponent.getCurrentUser();
        c.setCreateId(user.getId()).setCreateDate(LocalDateTime.now())
                .setUpdateId(user.getId()).setUpdateDate(LocalDateTime.now());
        this.insert(c);
    }

    @Override
    public String downTemplateData(String templateId, HttpServletResponse response) throws IOException {
        XSSFWorkbook workbook = new XSSFWorkbook(PoiUtil.getInputStream("/formTemplate/file/template/formsTemplate.xlsx"));
        SXSSFWorkbook book = new SXSSFWorkbook(workbook, 100);
        FormsTemplateConfig config = this.fetch(templateId);
        if (config == null) {
            throw new AppException("模板数据不存在！");
        }
        String params = config.getParams();
        if (StringUtils.isEmpty(params)) {
            return PoiUtil.getDownFileUrl(System.currentTimeMillis() + ".xlsx", book, response);
        }
        CellStyle cellStyle = PoiUtil.getCellStyle(book);
        List<String> list = new java.util.ArrayList<>(List.of("序号", "报表编号", "报表名称", "报表类别", "说明", "备注"));
        Collections.addAll(list, params.split(","));
        int cellIndex = 0;
        SXSSFSheet sheet = book.getSheetAt(0);
        SXSSFRow row = sheet.createRow(0);
        row.setHeight((short) 1200);
        for (String value : list) {
            SXSSFCell cell = row.createCell(cellIndex);
            cell.setCellValue(value);
            cell.setCellStyle(cellStyle);
            cellIndex ++;
        }
        return PoiUtil.getDownFileUrl(System.currentTimeMillis() + ".xlsx", book, response);
    }
}
